package com.heima.wemedia.controller.v1;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.wemedia.service.WmMaterialService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * <p>
 * 自媒体图文素材信息表 前端控制器
 * </p>
 *
 * @author itheima
 */
@Slf4j
@RestController
@RequestMapping("/api/v1/material")
public class WmMaterialController {

    @Autowired
    private WmMaterialService  wmMaterialService;

    @PostMapping("/list")
    public PageResponseResult findList(@RequestBody WmMaterialDto wmMaterialDto){
        Page<WmMaterial> wmMaterialPage= wmMaterialService.findList(wmMaterialDto);
        return new PageResponseResult(wmMaterialPage);
    }

    @PostMapping("/upload_picture")
    public ResponseResult uploadPicture(MultipartFile multipartFile){
        try {
            wmMaterialService.uploadPicture(multipartFile);
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"文件上传失败");
        }

        return ResponseResult.okResult("上传成功");
    }
    @GetMapping("/del_picture/{id}")
    public ResponseResult delPicture(@PathVariable("id") Integer id){


        return wmMaterialService.delPicture(id);
    }
    @GetMapping("/cancel_collect/{id}")
    public ResponseResult cancelCollect(@PathVariable("id") Integer id){
        LambdaUpdateWrapper<WmMaterial> wmMaterialLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        wmMaterialLambdaUpdateWrapper.set(WmMaterial::getIsCollection,0);
        wmMaterialLambdaUpdateWrapper.eq(WmMaterial::getId,id);
        wmMaterialService.update(wmMaterialLambdaUpdateWrapper);
        return ResponseResult.okResult("操作成功");
    }


    @GetMapping("/collect/{id}")
    public ResponseResult collect(@PathVariable("id") Integer id){
        LambdaUpdateWrapper<WmMaterial> wmMaterialLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        wmMaterialLambdaUpdateWrapper.set(WmMaterial::getIsCollection,1);
        wmMaterialLambdaUpdateWrapper.eq(WmMaterial::getId,id);
        wmMaterialService.update(wmMaterialLambdaUpdateWrapper);
        return ResponseResult.okResult("操作成功");
    }

}
